rm(list=ls())

##############################################################################
# FIGURE 2 SCRIPT
##############################################################################

# load packages
#library(maps)
#library(tidyverse)
#library(countrycode)

# load data 
load("data/eurepoc_year_attacks.RData")

world_map = map_data("world")

world_map = map_data("world") %>% 
  filter(! long > 180)

world_map <- subset(world_map, region != "Antarctica")

countries = world_map %>% 
  distinct(region) %>% 
  rowid_to_column()

countries$ccode <- countrycode(countries$region, origin = "country.name", destination = "cown", warn = F)
countries$ccode <- ifelse(countries$region == "Serbia", 345 , countries$ccode)

ccode <- unique(eurepoc_year_data$ccode[eurepoc_year_data$candidate == 1])
candidate <- rep(1, length(ccode))

candidate <- as.data.frame(cbind(ccode, candidate))

countries <- merge(countries, candidate, by = "ccode", all.x = T)
countries$candidate[is.na(countries$candidate)] <- 0

#candidate with events in spells 
non_event_spells <- aggregate(eurepoc_year_data$attacks, list(eurepoc_year_data$country_spell), mean)
names(non_event_spells)[1] <- "country_spell"
names(non_event_spells)[2] <- "mean_attacks"

candidate_check <- aggregate(eurepoc_year_data$candidate, list(eurepoc_year_data$country_spell), sum)
names(candidate_check)[1] <- "country_spell"
names(candidate_check)[2] <- "candidate_check"

non_event_spells <- merge(non_event_spells, candidate_check, by = "country_spell", all.x = T)

non_event_spells <- separate(non_event_spells, country_spell, into = c("ccode", "spell"), 
                             sep = "_")

ccode <- unique(non_event_spells$ccode[non_event_spells$candidate > 0 & non_event_spells$mean_attacks > 0])
event <- rep(1, length(ccode))

candidate_event <- as.data.frame(cbind(ccode, event))

countries <- merge(countries, candidate_event, by = "ccode", all.x = T)
countries$event[is.na(countries$event)] <- 0

countries$event <- factor(countries$event)

countries$legend <- rep("non-candidate")
countries$legend <- ifelse(countries$candidate ==1, "candidate", countries$legend)
countries$legend <- ifelse(countries$candidate ==1 & countries$event ==1, "candidate with variation", 
                           countries$legend)

countries$candidate <- ifelse(countries$region == "Serbia", 1, countries$candidate)

countries$ccode <- as.character(countries$ccode)

countries$legend <- factor(countries$legend, levels = c("non-candidate", "candidate", "candidate with variation"))

map2 <- countries %>% 
  ggplot(aes(fill = legend, map_id = region)) +
  geom_map(map = world_map) +
  expand_limits(x = world_map$long, y = world_map$lat) +
  scale_fill_grey(start = .8, end = .0011) +
  theme(axis.text.x=element_blank(),
        axis.ticks.x=element_blank(), axis.text.y=element_blank(), 
        axis.ticks.y=element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), panel.background = element_blank(), 
        legend.text = element_text(size=15), legend.position="bottom") +
  labs(x = "", y = "", fill=NULL) 

map2

# save pdf
ggsave(map2, filename = "output/fig2.pdf")

# total number of countries with variation 
length(unique(countries$ccode[countries$candidate == 1 & countries$event == 1]))

# percent countries with variation 
(length(unique(countries$ccode[countries$candidate == 1 & countries$event == 1]))/length(unique(countries$ccode[countries$candidate == 1])))*100

# script complete message 
print("fig2 complete")

